import requests
import logging

# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def send_request(session, url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
        }
        response = session.get(url, headers=headers)
        response.raise_for_status()  # 如果响应状态码不是200，会抛出异常
        return response
    except requests.HTTPError as http_err:
        logging.error(f"HTTP error occurred: {http_err}")
    except requests.RequestException as req_err:
        logging.error(f"Request exception occurred: {req_err}")
    return None

def fetch_and_print_cookies(url):
    session = requests.Session()
    response = send_request(session, url)
    if response:
        cookies = response.cookies
        for cookie in cookies:
            logging.info(f"Cookie: {cookie.name} = {cookie.value}")
        logging.info(f"Cookies as dict: {cookies.get_dict()}")

def fetch_with_session(url):
    session = requests.Session()
    response = send_request(session, url)
    if response:
        # 发送另一个请求，自动携带之前的Cookie
        response = send_request(session, url)
        return response
    return None

# 调用函数
url = 'https://movie.douban.com/subject/25861610/'
fetch_and_print_cookies(url)
response = fetch_with_session(url)
if response:
    logging.info(f"Second request successful with status code: {response.status_code}")
